package com.yinhai.mycat_test.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yinhai.mycat_test.VO.InsertOrderVO;
import com.yinhai.mycat_test.VO.PageVO;
import com.yinhai.mycat_test.entity.IDCard;
import com.yinhai.mycat_test.entity.Order;
import com.yinhai.mycat_test.entity.OrderItem;
import com.yinhai.mycat_test.entity.User;
import com.yinhai.mycat_test.mapper.read.OrderItemReadMapper;
import com.yinhai.mycat_test.mapper.write.OrderItemWriteMapper;
import com.yinhai.mycat_test.service.read.IDCardReadService;
import com.yinhai.mycat_test.service.read.OrderReadService;
import com.yinhai.mycat_test.service.read.UserReadService;
import com.yinhai.mycat_test.service.write.IDCardWriteService;
import com.yinhai.mycat_test.service.write.OrderWriteService;
import com.yinhai.mycat_test.service.write.UserWriteService;
import com.yinhai.mycat_test.tools.SleepTools;

import lombok.extern.log4j.Log4j2;

import org.junit.jupiter.api.Tag;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
@Log4j2
public class TransactionService {

    @Resource
    UserWriteService userWriteService;
    @Resource
    UserReadService userReadService;

    @Resource
    IDCardWriteService idCardWriteService;

    @Resource
    IDCardReadService idCardReadService;
    @Resource
    OrderWriteService orderWriteService;

    @Resource
    OrderReadService orderReadService;
    
    @Resource
    private OrderItemWriteMapper orderItemWriteMapper;
    
    @Resource
    private OrderItemReadMapper orderItemReadMapper;
    
    @Transactional
    public void batchInsertTable(List<User> userList, List<IDCard> cardList, List<Order> orderList, List<OrderItem> orderItemList){
        userWriteService.batchInsertUser(userList);
        idCardWriteService.batchInsertIDCard(cardList);
        SleepTools.second(5);
        orderWriteService.batchInsertOrder(orderList);
        orderItemWriteMapper.addListOrderItem(orderItemList);
    }
    
    @Transactional
    public void batchInsertOrderItem( List<OrderItem> orderItemList){
        orderItemWriteMapper.addListOrderItem(orderItemList);
    }

    @Transactional
    public void insertSingleRecordTable(User user, IDCard idCard, InsertOrderVO insertOrderVO){
        userWriteService.insertUser(user);
        idCardWriteService.insertIDCard(idCard);
        orderWriteService.insertOrderByUserId(insertOrderVO);
        SleepTools.ms(200);
    }
    
    @Transactional
    public void insertSingleRecord(User user, IDCard idCard,Order order,OrderItem orderItem){
        userWriteService.insertUser(user);
        idCardWriteService.insertIDCard(idCard);
        orderWriteService.insertOrder(order);
        orderItemWriteMapper.addOrderItem(orderItem);
    }

    @Transactional
    public void deleteTable(){
        userWriteService.deleteAllUser();
        idCardWriteService.deleteAllIDCard();
        orderWriteService.emptyOrder();
    }

    @Transactional
    public void tableCount(){
        int user  = userReadService.queryUserCount();
        int id = idCardReadService.queryIDCardCount();
        int order = orderReadService.queryOrderCount();
        int orderItem = orderItemReadMapper.queryOrdeItemrCount();
        log.info("userCount::{},id::{},order::{},orderItem::{}",user,id,order,orderItem);
    }


    @Transactional
    public void queryAllTable(){
        List<User> users = userReadService.queryAllUser();
       List<IDCard> idCards =  idCardReadService.queryAllIDCard();
       IPage<Order> page= orderReadService.listAllOrder(new PageVO(1,100));

        System.out.println("user::"+users+"\n"+"idCards::"+idCards+"\n"+"orderPage::"+page.getRecords());
    }
    
    @Transactional
    public void mixoperate1(List<User> userList,List<Long> idList){
        userWriteService.batchInsertUser(userList);
        orderReadService.queryOrderCount();
        log.info("begin insert or delete ......");
        SleepTools.second(5);
        idCardWriteService.batchDeleteIDCardById(idList);
    }
    
    @Transactional
    public void mixoperate2(){
        IPage<Order> page = orderReadService.listAllOrder(new PageVO(1, 100));
        log.info("page records={}",page.getRecords().size());
    }
    
    @Transactional
    public void mixoperate3(List<User> userList,List<Order> orderList){
        userWriteService.batchInsertUser(userList);
        idCardReadService.queryIDCardCount();
        log.info("begin insert or delete ......");
        SleepTools.second(5);
        orderWriteService.batchInsertOrder(orderList);
    }

}
